package com.hengxinguotong.zhihuichengjian.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.orhanobut.logger.Logger;

import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Created by Administrator on 2017/6/19.
 */

public class WelcomeActivity extends AppCompatActivity
{

    private WelcomeActivity mActivity;
    private boolean isLogined = false;
    private boolean isFirst = true;
    @BindView(R.id.app_name)
    HXTextView appName;

    private String uuid, userId;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startup);
        ButterKnife.bind(this);
        mActivity = this;
        appName.setText(getString(R.string.app_name)+"  V" + Utils.getVersionName(mActivity));
//        appName.setText("智慧城建 beta 8.0");
        uuid = SPUtil.getString(this, "uuid");
        userId = SPUtil.getString(this, "userId");
        isFirst = SPUtil.getBoolean(this, "isFirst");
        if (!TextUtils.isEmpty(uuid) && !TextUtils.isEmpty(userId))
        {
            isLogined = true;
//            设置极光推送别名
//            JPushInterface.setAlias(mActivity, userId, new TagAliasCallback() {
//                @Override
//                public void gotResult(int i, String s, Set<String> set) {
//                    Logger.d("userId---------" + SPUtil.getString(mActivity, "userId"));
//                }
//            });
            // 调用 Handler 来异步设置别名
            mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, userId));
        }
        mRunnable mRunnable = new mRunnable();
        handler.postDelayed(mRunnable, 500);
    }

    private class mRunnable implements Runnable
    {

        @Override
        public void run()
        {
            if (isLogined)
            {
                Intent intent = new Intent();
                intent.setClass(mActivity, MainActivity.class);
                startActivity(intent);
                finish();
                return;
            } else
            {
                Intent intent = new Intent();
                if (isFirst)
                {
                    intent.setClass(mActivity, GuideActivity.class);
                } else
                {
                    intent.setClass(mActivity, LoginActivity.class);
                }
                startActivity(intent);
                finish();
                return;
            }
        }
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        JPushInterface.onResume(getApplicationContext());
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        JPushInterface.onPause(getApplicationContext());
    }

    String TAG = "JPush";
    private final TagAliasCallback mAliasCallback = new TagAliasCallback()
    {
        @Override
        public void gotResult(int code, String alias, Set<String> tags)
        {
            String logs;
            switch (code)
            {
                case 0:
                    // 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后，以后不必再次设置了。
                    Logger.d("Set tag and alias success!code=" + code + "alias=" + alias + "tags=" + tags);
                    break;
                case 6002:
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
                    Log.i(TAG, logs);
                    // 延迟 60 秒来调用 Handler 设置别名
                    mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
                    break;
                default:
                    logs = "Failed with errorCode = " + code;
                    Log.e(TAG, logs);
            }
        }
    };
    private static final int MSG_SET_ALIAS = 1001;
    private final Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(android.os.Message msg)
        {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case MSG_SET_ALIAS:
                    Log.d(TAG, "Set alias in handler.");
                    // 调用 JPush 接口来设置别名。
                    JPushInterface.setAliasAndTags(getApplicationContext(), (String) msg.obj, null, mAliasCallback);
                    break;
                default:
                    Log.i(TAG, "Unhandled msg - " + msg.what);
            }
        }
    };
}
